package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.job.vo.IotMsgService;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.utils.DateTimeZoneConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Map;

/**
 * 设备模型信息Service业务层处理
 * 
 * @author zhangyan
 * @date 2025-06-02
 */
@Service
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper,DeviceData> implements IDeviceDataService
{
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 查询设备模型信息
     * 
     * @param id 设备模型信息主键
     * @return 设备模型信息
     */
    @Override
    public DeviceData selectDeviceDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询设备模型信息列表
     * 
     * @param deviceData 设备模型信息
     * @return 设备模型信息
     */
    @Override
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData)
    {
        return deviceDataMapper.selectDeviceDataList(deviceData);
    }

    /**
     * 新增设备模型信息
     * 
     * @param deviceData 设备模型信息
     * @return 结果
     */
    @Override
    public int insertDeviceData(DeviceData deviceData)
    {
        return save(deviceData)?1:0;
    }

    /**
     * 修改设备模型信息
     * 
     * @param deviceData 设备模型信息
     * @return 结果
     */
    @Override
    public int updateDeviceData(DeviceData deviceData)
    {
        return updateById(deviceData)?1:0;
    }

    /**
     * 批量删除设备模型信息
     * 
     * @param ids 需要删除的设备模型信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    @Autowired
    private DeviceMapper deviceMapper;


    /**
     * 删除设备模型信息信息
     * 
     * @param id 设备模型信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    @Override
    public void saveBathByContentStr(String contentStr) {
        //校验
        if (StrUtil.isEmpty(contentStr)){
            return;
        }
        JSONObject contentJsonObj = JSONUtil.parseObj(contentStr);
        String deviceId = contentJsonObj.getByPath("notify_data.header.device_id", String.class);

        //查询是否存在
        Device device = deviceMapper.selectOne(new LambdaQueryWrapper<Device>()
                .eq(Device::getIotId, deviceId));
        if (device == null){
            return;
        }

        //存在后将数据解析
        IotMsgService msgService = contentJsonObj.getByPath("notify_data.body.services[0]", IotMsgService.class);

        String eventTimeStr = msgService.getEventTime();
        LocalDateTime eventTime = LocalDateTime.now();
        if (StrUtil.isNotEmpty(eventTimeStr)){
            LocalDateTime eventTimeUTC = LocalDateTimeUtil.parse(eventTimeStr, "yyyyMMdd'T'HHmmss'Z'");
            eventTime = DateTimeZoneConverter.utcToShanghai(eventTimeUTC);
        }

        //构造插入数据
        List<DeviceData> list = new ArrayList<>();

        LocalDateTime finalEventTime = eventTime;
        msgService.getProperties().forEach((k, v) -> {
            DeviceData deviceData = BeanUtil.toBean(device, DeviceData.class);
            deviceData.setId(null);

            deviceData.setFunctionId(k);

            deviceData.setAlarmTime(finalEventTime);

            deviceData.setDataValue(v.toString());

            list.add(deviceData);

        });


        try {
            saveBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
